home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / lsdoor09.zip / EXAMPLE4.CPP < prev    next >
C/C++ Source or Header  |  1996-06-02  |  3KB  |  99 lines

  1. // Example4.Cpp - This is an example of how to setup and use a database
  2. //   with the LsDoor SDK.  This is also the only example project/IDE file.
  3. //
  4. // Databases can be a fairly advanced topic.
  5. // Setting up a database involves creating macros for it, and defining it.
  6. // Once that is done, accessing the database is sometimes easy, but with many
  7. // different variations.  Study this example, then take a good look at
  8. // Example4.h to understand how these databases work.
  9. //
  10.  
  11. #define MainModule
  12. #include "LsDoor.H"
  13. #include "Example4.H"
  14.  
  15. char *errorModule( void ){ return "Example4 Databases"; }
  16.  
  17.  
  18. void main( void )
  19. {
  20.   if( !DoorInit( doorsysDISABLE ) ) exit(1);      // Get things started...
  21.  
  22.   display( "\n@CW@celcome %s.\n", user.handle );
  23.  
  24.   // Presented now are several different methods of loading data from your
  25.   // database.  You would only need to use one of these cases in a real
  26.   // program, however for this example we are including multiple.
  27.  
  28.   // Case 1 - See if we can find the user's key.  For a user/player database
  29.   //   such as this, this is probably the best technique.
  30.  
  31.   open_player();     // Open the file.
  32.   if( !find_player( user.handle ) )
  33.   {
  34.     display("\n  @Y You have no key.  @R You are new here.");
  35.   } else {
  36.     display("\n  @Y Found your key." );
  37.   }
  38.  
  39.   // Case 2 - Load using next_player()...
  40.  
  41.   test found = false;
  42.  
  43.   reset_player();           // Start at the beginning of the database...
  44.   while( next_player() )    // ...And load players until we run out.
  45.   {
  46.     if( !strcmpi( user.handle, player.bbshandle ) ){ found = true; break; }
  47.   }
  48.   if( !found ){
  49.     display("\n  @Y You have no records.  @R You are a new user." );
  50.   } else {
  51.     display("\n  @Y Found your record." );
  52.   }
  53.  
  54.   // Case 3 - Load using load_player()...  This can be slower than the other
  55.   // methods, however it is the only way to load your records in any
  56.   // ordered manor.
  57.  
  58.   unsigned long num = 0;
  59.   found = false;
  60.  
  61.   reset_player();
  62.   while( !end_player() ){    // Loop until past the highest 'num'
  63.     if( !load_player( num++ ) ) continue;
  64.     if( !strcmpi( user.handle, player.bbshandle ) ){ found = true; break; }
  65.   }
  66.   if( !found ){
  67.     display("\n  @Y Couldn't load you.  @R You are a new user." );
  68.   } else {
  69.     display("\n  @Y Loaded your records just fine." );
  70.   }
  71.  
  72.   char c;
  73.   while( online ){
  74.     display( "\n@CA@cdd your name to the player database?  @w(@GY@w/@gN@w) ");
  75.     c = toupper(s_in());
  76.     if( c == 'Y' ){ echo_yn( c );
  77.       strcpy( player.bbshandle, user.handle );
  78.       display("\n@CW@chat alias will use here:  @W" );
  79.       s_gets( player.handle, 20 );
  80.       player.points = 0;    // Fill in some dummy data
  81.       player.power  = 2;
  82.       add_player();
  83.  
  84.       display("\n@RI@rt is done.\n\n" );
  85.       break;
  86.     }
  87.     if( c == 'N' ){ echo_yn( c ); break; }
  88.   }
  89.   close_player();
  90.   display("\n@GA@gu revior.");
  91.  
  92.   exit(0);                                        // Always use exit()
  93. }
  94.  
  95. // End of Example4.Cpp
  96.  
  97.  
  98.  
  99.